package neu.dreamera.activity;

import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import neu.dreamera.R;
import neu.dreamera.utils.AsyncGetDataUtil;
import neu.dreamera.utils.Myadapter;
import neu.dreamera.view.ReFlashlistView;

/**
 * Created by root on 16-6-27.
 */
public class PointActivity extends Activity implements ReFlashlistView.IReflashListener{

    private static final String TAG="PointActivity";

    private int point_id;
    private String point_name;
    private String point_longitude;
    private String point_latitude;

    private TextView point_name_textview;
    private TextView point_position_textview;

    private ReFlashlistView picture_listview;
    ArrayList<HashMap<String,Object>> listItem;

    private Button add_picture;
    private String cross_pictures;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.point_item);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        point_id = bundle.getInt("point_id");
        point_name = bundle.getString("name");
        point_longitude = bundle.getString("longitude");
        point_latitude = bundle.getString("latitude");
        cross_pictures = bundle.getString("cross_pictures");

        point_name_textview = (TextView) findViewById(R.id.point_name);
        point_name_textview.setText(point_name);
        point_position_textview = (TextView) findViewById(R.id.point_position);
        point_position_textview.setText(point_longitude + "," + point_latitude);

        add_picture = (Button) findViewById(R.id.addPicturebutton);//here

        //picture list
        Log.d(TAG,"show cross_pictures");
        picture_listview = (ReFlashlistView)findViewById(R.id.piclistView);
        Myadapter myadapter;

        try {
            Log.d(TAG,"cross_pictures  is "+cross_pictures);
            listItem = AsyncGetDataUtil.decodeCrossPicturesJsonToPoint(cross_pictures);//problem is here
            Log.d(TAG,"listItem is "+listItem);
            List<String> URL = new ArrayList<String>();
            List<String> title = new ArrayList<String>();
            for (int i = 0; i < listItem.size(); i++) {
                URL.add((String) listItem.get(i).get("url"));
                title.add((String) listItem.get(i).get("title"));
            }
            Log.d(TAG,"the URL is"+URL);
            Log.d(TAG,"The title is"+title);
            myadapter = new Myadapter(PointActivity.this, URL, title, picture_listview);
            // 添加并显示
            picture_listview.setAdapter(myadapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void onStart(){
         super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.d(TAG,"onResume");

        add_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LocationManager locationManager;
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                List<String> providerList = locationManager.getProviders(true);
                if (providerList.contains(LocationManager.GPS_PROVIDER)){
                    Intent intent = new Intent();
                    intent.setClass(PointActivity.this, CameraActivityplus.class);
                    intent.putExtra("point_id",point_id);
                    PointActivity.this.startActivity(intent);
                }else{
                    Toast.makeText(PointActivity.this, "请打开GPS来确定您的位置!", Toast.LENGTH_SHORT).show();
                }

                // intent.setClass(PointActivity.this, AddPictureActivity.class);

              /*  intent.putExtra("point_id", point_id);
                intent.putExtra("name", point_name);
                //intent.putExtra( "cross_pictures" ,cross_pictures);
                //intent.putExtra("longtitude",point_longitude);
                //intent.putExtra("latitude",point_latitude);*/
            }
        });

        /*
        point_name_textview.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                    point_name_textview.setEnabled(true);
            }
        });*/
        picture_listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                    HashMap<String,Object> item=listItem.get(position-1);
                    //int pic_id= (int) item.get("id");
                    String pic_title= (String) item.get("title");
                    String picture= (String) item.get("picture");
                    String datetime= (String) item.get("date");
                    String time_str= (String) item.get("time_str");
                    String detail_title= (String) item.get("detail_title");
                    String like_count= (String) item.get("like_count");
                    //String location= (String) item.get("location");
                    Intent intent = new Intent();
                    intent.setClass(PointActivity.this, PictureActivity.class);
                    intent.putExtra("title",pic_title);
                    //intent.putExtra("id",pic_id);
                    intent.putExtra("picture",picture);
                    intent.putExtra("datetime",datetime);
                    intent.putExtra("time_str",time_str);
                    intent.putExtra("detail_title",detail_title);
                    intent.putExtra("like_count",like_count);
                    //intent.putExtra("location",location);
                    PointActivity.this.startActivity(intent);
            }
        });

    }
    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG,"onPause");
    }
    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG,"onStop");
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
    @Override
    protected void onRestart(){
        super.onRestart();
        Log.d(TAG,"onRestart");
    }

    @Override
    public void onReflash() {
        /*
        Handler handler=new Handler();
        handler.post(Runnable(){
           public void run(){

            }
        });*/
    }
}
